package cn.itcast.itcaststore.dao.Impl;

import cn.itcast.itcaststore.dao.UserDao;
import cn.itcast.itcaststore.domain.User;
import cn.itcast.itcaststore.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class IUserDao implements UserDao {

    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void addUser(User add_user) {
        String addUser_sql = "insert into user values(null ,?,?,?,?,?,?,default,default,default,default)";
        jdbcTemplate.update(addUser_sql,
                add_user.getUsername(),
                add_user.getPassword(),
                add_user.getGender(),
                add_user.getEmail(),
                add_user.getTelephone(),
                add_user.getIntroduce()
                );
    }

    @Override
    public User getUser(String username) {
        String getUser_sql = "select*from user where username=?";
        List<User> list = jdbcTemplate.query(getUser_sql, new BeanPropertyRowMapper<User>(User.class), username);
        User user = null;
        for (User user_list : list) {
            user=user_list;
        }
        return user;
    }

    @Override
    public void modifyUser(User modify_user) {
        if (modify_user.getUsername()!=null){
            String modify_sql = "update user set password=?,gender=?,telephone=? where username=?";
            jdbcTemplate.update(modify_sql,modify_user.getPassword(),modify_user.getGender(),modify_user.getTelephone(),modify_user.getUsername() );
        }
    }
}
